#Hauenstein Rebel Substitutes and Amnesty Main Replication File#
#Setup####
library(multiwayvcov)
library(lmtest)
library(brglm)
library(tidyverse)
library(coefplot)
library(readstata13)
library(corrplot)
library(viridis)
library(stargazer)
library(car)
library(margins)
library(xtable)
library(Matching)
library(rbounds)
library(MatchIt)
library(reshape2)
library(beepr)
library(BAS)
library(GGally)
library(tictoc)
library(Zelig) 
#Zelig is not working on CRAN (for me), installed manually.
#install.packages('https://cran.r-project.org/src/contrib/Archive/Zelig/Zelig_5.1.6.1.tar.gz', repos = NULL, type = 'source', dependencies = T)

#Info
print(sessionInfo())

#Set directory to replication file location
#setwd()

#Notes
#There are counters (cat) for the loops that execute the bootstraps
#These have been commented out to avoid printing a huge number of counters
#when this file was knit to create the log.  

#Measure time to run 
tic()

#Source helper functions - see readme
source("Bootstrap.R")
source("InteractionPlot.R")


#Descriptives- Figure 1 #####
source("Figure 1.R", echo=T)

#Descriptives - Figures 2 and 3 #####
source("Figures 2 and 3.R", echo=T)

#Main Results - Provision Models#####
#There will be a delay while bootstraps are estimated
#Will beep on completion
#Produces the following
  #Figure 4
  #Table 4
  #Figure 6 
  #Figure 7
  #Figures 11 and 12
  #Figures 13 and 14
source("Provision Analysis.R", echo=T)
beep(4)

#Matching Analysis ####
#Produces the following
  #Figure 5 and Rosenbaum Bounds
  #Tables 5 and 6 - Balance Statistics
  #Figures 8 and 9 - QQ Plots
  #Figure 10 - Stochastic Simulation of Full Propensity Match
source("Provision Matching Analysis.R", echo=T)

#Implementation Analysis #####
#There will be several delays for bootstraps
#Will beep on completion
#Produces the following
  #Figure 6 - Main Implementation Results
  #Table 7 - Regression Table
  #Figures 16-18 - Alternative Substitute Measures
  #Figures 19-20 - Alternative Specifications
  #Figures 21-22 - BMA for Implementation
source("Implementation Analysis.R", echo=T)
beep(4)

#Appendix - Amnesty Provision Descriptives
#Produces the following
  #Figures A1 and A2
  #Table 1
  #Table 2
  #Figure A4
source("Provision Descriptives.R", echo=T)

#Appendix - Implementation Descriptives
#Produces the following
  #Table 2
  #Figure A3
  #Figure A5
source("Implementation Descriptives.R", echo=T)

#Appendix - Implementation Placebo Test
#Produces Figure 15
source("Placebo Test.R", echo=T)

#Show run time
toc()

rm(list=ls())
